package com.qpx.eurekaconsumerfeign.web;


import com.qpx.dto.UserDto;
import com.qpx.eurekaconsumerfeign.service.UserService;
import com.qpx.eurekaconsumerfeign.utils.UploadUtils;
import com.qpx.vo.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RefreshScope
public class UserController {


    @Autowired
    UploadUtils uploadUtils;
    @Autowired
    UserService userService;


    @Value("${server.port}")
    private String port;

    @RequestMapping("/user/list")
    public ResultEntity getUserList(UserDto userDto){
        log.info("-------------------------------{}",port);
        return userService.getUserList(userDto);
    }


    @RequestMapping(value = "/user/save",method = RequestMethod.POST)
    public ResultEntity saveUser(@ModelAttribute UserDto userDto) throws IOException {
        List<String> upload = uploadUtils.upload(userDto.getFiles());
        String imageUrl = upload.get(0);
        userDto.setFiles(null);
        userDto.setUserface(imageUrl);
        userService.save(userDto);
        return ResultEntity.ok();
    }
}
